package com.ruanda.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruanda.domain.Order;
import com.ruanda.model.Result;
import com.ruanda.service.OrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 订单业务模块控制层
 */
@Tag(name = "订单业务接口管理")
@RequestMapping("order/order")
@RestController
public class SysOrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 多条件分页查询订单
     *
     * @param current     当前页
     * @param size        数量
     * @param orderNumber 订单号
     * @param status      状态
     * @param startTime   开始时间
     * @param endTime     结束时间
     * @return
     */
    @Operation(summary = "多条件分页查询订单")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('order:order:page')")
    public Result<Page<Order>> loadOrderPage(@RequestParam Long current,
                                             @RequestParam Long size,
                                             @RequestParam(required = false) String orderNumber,
                                             @RequestParam(required = false) Integer status,
                                             @RequestParam(required = false) Date startTime,
                                             @RequestParam(required = false) Date endTime) {
        // 创建订单分页对象
        Page<Order> page = new Page<>(current, size);
        // 多条件分页查询订单
        page = orderService.queryOrderPage(page, orderNumber, status, startTime, endTime);
        return Result.success(page);
    }

    @Operation(summary = "根据订单编号查询订单详情")
    @GetMapping("orderInfo/{orderNumber}")
    @PreAuthorize("hasAuthority('order:order:info')")
    public Result<Order> loadOrderDetail(@PathVariable Long orderNumber) {
        Order order = orderService.queryOrderDetailByOrderNumber(orderNumber);
        return Result.success(order);
    }

    @Operation(summary = "导出销售记录")
    @GetMapping("soldExcel")
    @PreAuthorize("hasAuthority('order:order:soldExcel')")
    public Result<String> exportSoleOrderRecordExcel() {
        // 查询所有销售记录
        List<Order> list = orderService.list(new LambdaQueryWrapper<Order>()
                .orderByDesc(Order::getCreateTime)
        );
        System.out.println(list);
        // response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        // response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        // String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\\+", "%20");
        // response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        // EasyExcel.write(response.getOutputStream(), Order.class).sheet("模板").doWrite(list);
        String fileName = "D:\\upload-images\\" + System.currentTimeMillis() + ".xlsx";
        EasyExcel.write(fileName,Order.class).sheet("模板111").doWrite(list);
        return Result.success(null);
    }
}